package com.tianqu.tms.www.member.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.www.member.actionform.MemberInputActionForm;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 
 * 会员基本信息修改分发Action 用于处理图片
 * 
 * 
 * @author tianshifu
 *
 */
public class MemberUDBaseDispatchAction extends TmsAction {
	
	private static final String LOADIMG = "imgLoad";//跳转到图片上传
	private static final String TOCONFIRM = "toConfirm";//保存确认
	private static final String POSITION = "position";//图片位置
	
	private static final String IMGLIST = "imgList";//图片list
	
	private Logger logger = Logger.getLogger(MemberUDBaseDispatchAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		String actionForward = "";
		//根据页面按钮选择跳转页面
		MemberInputActionForm memberForm = (MemberInputActionForm)form;
		
		MemberInputViewHelper viewhelp =new MemberInputViewHelper();
		
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null)
		{
			viewhelp =(MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		}
		//给ViewHelper赋值
		viewhelp = setViewFromForm(viewhelp,memberForm);	
		
		//给图片set值
		List<CarImage> imgList = new ArrayList<CarImage>();
		if(uc.getAttribute(IMGLIST)!=null)
		{
			imgList = (List<CarImage>)uc.getAttribute(IMGLIST);
		}
//		view.setOrgPhoto(form.getOrgPhoto());
//		view.setBusinessPhoto(form.getBusinessPhoto());//这三张图片存在别的表中
//		view.setCardPhoto(form.getCardPhoto());
		CarImage carImage = new CarImage();
		if(viewhelp.getOrgPhoto()!=null && !"".equals(viewhelp.getOrgPhoto()))
		{
			carImage =new CarImage();
			carImage.setImgKey("1");
			carImage.setImgPath(viewhelp.getOrgPhoto());
			imgList.add(carImage);
		}
		if(viewhelp.getBusinessPhoto()!=null && !"".equals(viewhelp.getBusinessPhoto()))
		{
			carImage =new CarImage();
			carImage.setImgKey("2");
			carImage.setImgPath(viewhelp.getBusinessPhoto());
			imgList.add(carImage);
		}
		if(viewhelp.getCardPhoto()!=null && !"".equals(viewhelp.getCardPhoto()))
		{
			carImage =new CarImage();
			carImage.setImgKey("3");
			carImage.setImgPath(viewhelp.getCardPhoto());
			imgList.add(carImage);
		}
		if(viewhelp.getMemberZipCode()!=null && !"".equals(viewhelp.getMemberZipCode()))
		{
			carImage =new CarImage();
			carImage.setImgKey("5");
			carImage.setImgPath(viewhelp.getMemberZipCode());
			imgList.add(carImage);
		}
		uc.setAttribute(IMGLIST, imgList);
		
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad1()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad2()))
		{
			// 
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.TWO);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad3()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.THREE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad5()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.FIVE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getToSave()))
		{
			//确认页面
			actionForward = TOCONFIRM;
		}
		//进到页面之前需要将页面信息set到uc中用于保存
		
		
		uc.setAttribute(TmsConstants.VIEWHELP,viewhelp);//将viewhelp放到uc中
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}
	
	/**
	 * 
	 * 将form表单内容set到viewhelp中
	 * 
	 * @return
	 */
	private MemberInputViewHelper setViewFromForm(MemberInputViewHelper view,MemberInputActionForm form)
	{
		//商户名称
		view.setRealName(form.getRealName());
		//商户短名
		view.setShortName(form.getShortName());
		//商户名称拼音
		view.setRealNamePy(form.getRealNamePy());
		//联系人
		view.setContactOwner(form.getContactOwner());
		//联系电话
		view.setContactPhone(form.getContactPhone());
		//法人地址
		view.setOrgNo(form.getOrgNo());
		//法人名称		
		view.setLegal(form.getLegal());
		//法人电话
		view.setPhone(form.getPhone());
		//法人身份证
		view.setCardNo(form.getCardNo());
		//法人性别
		view.setRegisteredCapital(form.getRegisteredCapital());
		//法人生日
		view.setSettlePhone(form.getSettlePhone());
		//备注
		view.setCarPortInstr(form.getCarPortInstr());

		return view;
	}
}
